

cc.Class({
    extends: cc.Component,

    properties: {
        timeLabel : cc.Label,
        coinLabel : cc.Label,
        progress : cc.ProgressBar,
        maxTime : 3600, //最大时长，单位秒
        scale : 3,
        priceTime : 10 //转一圈需要的时长，单位秒
    },
    cleanTime(){
        this.startTime = Date.now();
        this._saveInitTime();
    },

    getCoins(){
        return this.coins
    },

    _saveInitTime(){
        cc.sys.localStorage.setItem('onlinereward_starttime',this.startTime);
    },

    onEnable(){
        var startTime = cc.sys.localStorage.getItem('onlinereward_starttime');
        if(!startTime){
            this.startTime = Date.now();
            this._saveInitTime()
        } else {
            this.startTime = startTime;
        }
        this.coins = 0;
        this.lastCount = -1
    },
    update(){
        var ctime = Date.now();
        var dtime = Math.min(ctime - this.startTime,this.maxTime*1000);
        var count = Math.floor(dtime/(this.priceTime*1000))

        var progressTime = dtime%(this.priceTime*1000)
        this.progress.progress = 1-progressTime/(this.priceTime*1000)
        if(this.lastCount == count){
            return;
        }
        this.coins = Math.floor(count * this.scale * this.priceTime)
        this.coinLabel.string = this.getKV(this.coins);
        var str = '';

        var ltime = this.maxTime*1000 - dtime
        var t = Math.floor(ltime / 3600000)
        str += (t >=10 ? t : '0'+t)+':'
        var min = Math.floor((ltime - t*3600000)/60000)
        str += (min >=10 ? min : '0'+min)
        this.timeLabel.string = str
    },
    getKV(d){
        if(d < 1000){
            return d;
        }
        var a = d/1000;
        if(d > Math.floor(a)*1000){
            return a.toFixed(1)+'k'
        } else {
            return a+'k'
        }
    }
});
